//
//  APHomeViewController.m
//  AqualPolo
//
//  Created by dong on 2024/5/1.
//

#import "APHomeViewController.h"
#import "APHomeView.h"
#import "APLoginManager.h"
#import "APHomeDataManager.h"
#import "APHomeBallAlertController.h"
#import "APUnlockAlertViewController.h"
#import "APDiscoveryDataManager.h"

@interface APHomeViewController ()<APHomeBallAlertControllerDeleagte>
@property (nonatomic, strong) APHomeView *homeView;

@end

@implementation APHomeViewController

- (APHomeView *)homeView{
    if (!_homeView) {
        _homeView = [[APHomeView alloc] init];
        [_homeView.clickButton addTarget:self action:@selector(clickButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [_homeView.volleyballView.noviceDancerItem addTarget:self action:@selector(noviceDancerItemDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_homeView.volleyballView.intermediateWarriorItem addTarget:self action:@selector(intermediateWarriorItemDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_homeView.volleyballView.seniorDominatorItem addTarget:self action:@selector(seniorDominatorItemDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_homeView.volleyballView.eliteLeaderItem addTarget:self action:@selector(eliteLeaderItemDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_homeView.volleyballView.legendKingItem addTarget:self action:@selector(legendKingItemDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_homeView.volleyballView.supremeGodItem addTarget:self action:@selector(supremeGodItemDidClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _homeView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.homeView];
    [self.homeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    if (![[APHomeDataManager shareInstance] isBallUnlock:APBallTypeNoviceDancer]) {
        [self performSelector:@selector(showFirstLoginGetBallAlert) withObject:nil afterDelay:0.1];
    }
}



- (void)clickButtonDidClick:(UIButton *)sender{
    APBallType ballType = [APHomeDataManager shareInstance].currentSelectedBall;
    bool isUnlock =  [[APHomeDataManager shareInstance] isBallUnlock:ballType];
    if (isUnlock) {
        APBallType type =  [[APHomeDataManager shareInstance] currentUseBallType];
        if (type != ballType) {//use immediately
            [[APHomeDataManager shareInstance] updateCurrentUseBall:ballType];
            [self reloadData];
            [self.view ShowProgressOnlyLabel:@"Used successfully."];
        }
    }else{//To unlock
        [self toUnlockBall:ballType];
        
    }
}

- (void)toUnlockBall:(APBallType)ballType{
    APHomeBallAlertController *alertVC = [[APHomeBallAlertController alloc] init];
    alertVC.ballType = ballType;
    alertVC.delegate = self;
    [self presentViewController:alertVC animated:true completion:nil];
}


- (void)ballUnlockSuccess:(APBallType)ballType{
    [self reloadData];
}


- (void)ballDidClick:(APBallType)ballType{
    [APHomeDataManager shareInstance].currentSelectedBall = ballType;
    [self reloadData];
    bool isUnlock =  [[APHomeDataManager shareInstance] isBallUnlock:ballType];
    self.homeView.volleyballView.selectedBallView.ballType = ballType;
    self.homeView.volleyballView.selectedBallView.coverView.hidden = isUnlock;
    self.homeView.bubbleView.hidden = isUnlock;
    if (isUnlock) {
        APBallType type =  [[APHomeDataManager shareInstance] currentUseBallType];
        NSString *title = type == ballType ? @"Using":@"use immediately";
        [self.homeView.clickButton setTitle:title forState:UIControlStateNormal];
    }else{
        self.homeView.bubbleView.ballType = ballType;
        [self.homeView.clickButton setTitle:@"To unlock" forState:UIControlStateNormal];
    }
}


//1.Novice Dancer 2.Intermediate Warrior 3.Senior Dominator 4.Elite Leader 5.Legend King 6.Supreme God
- (void)noviceDancerItemDidClick{
    [self ballDidClick:APBallTypeNoviceDancer];
}

- (void)intermediateWarriorItemDidClick{
    WS(ws);
    [self.view ShowProgress];
    [[APDiscoveryDataManager shareInstance] loadData:^{
        [self.view removeProgress];
        [ws ballDidClick:APBallTypeIntermediateWarrior];
    }];
}

- (void)seniorDominatorItemDidClick{
    [self ballDidClick:APBallTypeSeniorDominator];
}

- (void)eliteLeaderItemDidClick{
    [self ballDidClick:APBallTypeLeader];

}

- (void)legendKingItemDidClick{
    [self ballDidClick:APBallTypeLegendKing];
}

- (void)supremeGodItemDidClick{
    [self ballDidClick:APBallTypeSupremeGod];
}



- (void)reloadData{
    APUserModel *userModel = [[APLoginManager shareInstance] userInfo];
    [self.homeView.userView.avatarImageView sd_setImageWithURL:[NSURL URLWithString:userModel.avatarUrl]];
    self.homeView.userView.nameLabel.text = userModel.nickname;
    self.homeView.userView.ballImageView.image = [[APHomeDataManager shareInstance] currentUseBallImage];
    self.homeView.userView.ballNameLabel.text = [[APHomeDataManager shareInstance] currentUseBallName];
    
    APBallType currentSelectedType = [[APHomeDataManager shareInstance] currentSelectedBall];
    BOOL isUnlock = [[APHomeDataManager shareInstance] isBallUnlock:currentSelectedType];
    self.homeView.volleyballView.selectedBallView.coverView.hidden = isUnlock;
    self.homeView.bubbleView.hidden = isUnlock;
    if (isUnlock) {
        APBallType type =  [[APHomeDataManager shareInstance] currentUseBallType];
        NSString *title = type == currentSelectedType ? @"Using":@"use immediately";
        [self.homeView.clickButton setTitle:title forState:UIControlStateNormal];
    }else{
        self.homeView.bubbleView.ballType = currentSelectedType;
        [self.homeView.clickButton setTitle:@"To unlock" forState:UIControlStateNormal];
    }
}


- (void)showFirstLoginGetBallAlert{
    [[APHomeDataManager shareInstance] updateCurrentUseBall:APBallTypeNoviceDancer];
    [[APHomeDataManager shareInstance] unlockBall:APBallTypeNoviceDancer];
    [self reloadData];
    APHomeBallAlertController *alertVC = [[APHomeBallAlertController alloc] init];
    alertVC.ballType = APBallTypeNoviceDancer;
    [self presentViewController:alertVC animated:true completion:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = true;
    [self reloadData];
}


@end
